<template>
  <div class="todo-list-container">
    <el-card class="todo-card" v-for="(todo, index) in todos" :key="index">
      <div class="todo-header">
        <span class="todo-title">{{ todo.title }}</span>
        <span class="todo-date">{{ (todo.date)?$moment(todo.date).format('YYYY-MM-DD'):''}}</span>
      </div>
      <div class="todo-priority" :class="`priority-${todo.priority}`">
        {{ todo.priority === 'high'? '高' : todo.priority ==='medium'? '中' : '低' }}
      </div>
      <div class="todo-actions">
        <el-button v-show="todo.status === '未完成'" size="mini" type="success" @click="completeTodo(todo)">完成</el-button>
        <el-button size="mini" type="danger" @click="deleteTodo(todo)">删除</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from "@/utils/request";
import dayjs from "@/utils/dayjs";

export default {
  name: "TodoContent",
  data() {
    return {
      todos: [
        {
          title: '完成项目报告',
          date: '2024-12-31',
          priority: 'high'
        },
        {
          title: '参加团队会议',
          date: '2024-12-15',
          priority:'medium'
        }
      ],
      allTodos:[],
      newTodo: {
        title: '',
        date: '',
        priority: ''
      }
    }
  },
  mounted() {
    this.loadData();
  },
  computed:  {
    todoDate(){
      return this.$store.state.todoDate;
    }
  },
  watch:{
    todoDate(newValue, oldValue){
      console.log('newValue', newValue);
      console.log('oldValue', oldValue);
      var date = dayjs(newValue).format('YYYY-MM-DD HH:mm:ss');
      // 根据todoDate过滤todos数据
      this.todos = this.allTodos.filter(item => {
        return date === item.date;
      });
    }
  },
  methods:{
    completeTodo(todo) {
      request({
        url: '/todo',
        method: 'put',
        data: {
          id: todo.id,
          todoStatus: '已完成'
        }
      }).then(response => {
        if (response && response.data) {
          this.loadData();
        }
      }).catch(error => {
        console.log('error', error);
        this.$message.error('更新数据失败')
      })
    },
    deleteTodo(todo) {
      const idList = [];
      idList.push(todo.id);
      request({
        url: '/todo',
        method: 'delete',
        params: {
          idList: idList.join(",")
        }
      }).then(response => {
        if (response && response.data) {
          this.loadData();
        }
      }).catch(error => {
        console.log('error', error);
        this.$message.error('删除数据失败')
      })
    },
    loadData() {
      request({
        url: '/todo',
        method: 'get',
      }).then(response => {
        if (response && response.data) {
          const tempList = [];
          response.data.records.forEach(item => {
            tempList.push({
              id: item.id,
              priority: item.todoPrority,
              title: item.todoName,
              status: item.todoStatus,
              date: item.todoDate
            })
          })
          console.log('tempList', tempList);
          this.todos = tempList;
          this.allTodos = tempList;
        }
      }).catch(error => {
        console.log('error', error);
        this.$message.error('加载数据失败')
      })
    }
  }
}
</script>

<style scoped>
.todo-list-container {
  padding: 20px;
}

.todo-card {
  margin-bottom: 15px;
  padding: 15px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}

.todo-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.todo-title {
  font-size: 16px;
  font-weight: bold;
}

.todo-date {
  color: #999;
  font-size: 14px;
}

.todo-priority {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 3px;
  color: white;
}

.priority-high {
  background-color: #f56c6c;
}

.priority-medium {
  background-color: #e6a23c;
}

.priority-low {
  background-color: #909399;
}

.todo-actions {
  margin-top: 10px;
  text-align: right;
}

.input-new-todo {
  width: 200px;
  margin-right: 10px;
  margin-bottom: 10px;
}
</style>